﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace IrisGenerator.Framework.Reflection
{
    public class Property
    {
        public static object GetPropertyValue(object obj, string propertyName)
        {
            if (obj == null || string.IsNullOrEmpty(propertyName))
                return null;

            //Check for object hierarchy
            string[] objects = propertyName.Split(".".ToCharArray());
            PropertyInfo pi = null;

            object value = obj;

            Type type = obj.GetType();

            if (objects != null)
            {
                foreach (string str in objects)
                {
                    pi = type.GetProperty(str);
                    value = pi.GetValue(value, null);
                    if (value == null)
                        return null;
                    type = value.GetType();
                }
            }
            else
            {
                pi = type.GetProperty(propertyName);
                value = pi.GetValue(value, null);
            }

            return value;
        }
    }
}
